package com.delaphone.fadmin.action;

import com.delaphone.fadmin.service.CustomerService;
import com.delaphone.flocash.model.FUser;
import com.delaphone.flocash.web.util.Validate;
import com.opensymphony.xwork2.Action;

/**
 * Login Action Controller
 * @author dongtv
 */
public class EditCustomerAction extends CoreAction {

    private String cus;
    private String email;
    private String firstName;
    private String lastName;
    private String city;
    private String number;
    private String country;
    private CustomerService customerService;
    private String message;
    //private FUser customer;

    public String index() {
        logger.info("username:" + cus);
        if (cus == null) {
            return Action.ERROR;
        }
        FUser customer = null;
        try {
            customer = customerService.findCustomers(null, null, email, null, null, null, null, 1, 1).get(0);
        } catch (Exception ex) {
        }
        if (customer == null) {
            return Action.ERROR;
        }
        setEmail(cus);
        setFirstName(customer.getFirstName());
        setLastName(customer.getLastName());
        setNumber(customer.getPhoneNumber());
        setCity(customer.getCity());
        setCountry(customer.getCountryCode());
        logger.info("email:"+getEmail()+"|country:"+customer.getCountryCode());
        return Action.INPUT;
    }

    public String suspendCustomer() {
        // logger.info("suppend user: "+usN);
        if (cus == null) {
            return Action.SUCCESS;
        }
        FUser customer = null;
        try {
            customer = customerService.findCustomers(null, null, email, null, null, null, null, 1, 1).get(0);
        } catch (Exception ex) {
        }
        if (customer == null) {
            return Action.SUCCESS;
        }
        
        customer.setSuspended("yes");
        if (customerService.updateCustomer(customer)) {
            message = "User " + cus + " suspended succesfully! ";

        }

        return Action.SUCCESS;
    }

    public String deactiveCustomer() {
        // logger.info("suppend user: "+usN);
        if (cus == null) {
            return Action.SUCCESS;
        }
        FUser customer = null;
        try {
            customer = customerService.findCustomers(null, null, email, null, null, null, null, 1, 1).get(0);
        } catch (Exception ex) {
        }
        if (customer == null) {
            return Action.SUCCESS;
        }
        
        customer.setActive("no");
        if (customerService.updateCustomer(customer)) {
            message = "User " + cus + " deactive succesfully! ";

        }


        return Action.SUCCESS;
    }

    public String saveEdit() throws Exception {
        //Step1: Validate



        if (!Validate.isPhoneNumber(number)) {
            addFieldError("number", "invalid phone number");
        }

        if (!getFieldErrors().isEmpty()) {
            addActionError("invalid information!");
        }
        if (!getActionErrors().isEmpty()) {
            return Action.INPUT;
        }
//        logger.info("Email:" + email);
//
//        //Step2: check username & password (will add later)
//
        FUser customer = customerService.findCustomers(null, null, email, null, null, null, null, 1, 1).get(0);
        customer.setFirstName(firstName);
        customer.setLastName(lastName);
        customer.setPhoneNumber(number);
        customer.setCity(city);
        customer.setCountryCode(country);

        if (customerService.updateCustomer(customer)) {
            message = "Customer " + email + " updated succesfully! ";
            return SUCCESS;
        } else {
            addActionError("Update Customer fail");
            return INPUT;
        }
    }

    // <editor-fold defaultstate="collapsed" desc="get and set methods">
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCus() {
        return cus;
    }

    public void setCus(String cus) {
        this.cus = cus;
    }

    public CustomerService getCustomerService() {
        return customerService;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
    // </editor-fold>
}
